package com.zy.world

import android.os.Bundle
import android.widget.RadioGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.zy.activity.BaseActivity
import com.zy.assemble.fragment.AndroidFragment
import com.zy.assemble.fragment.SettingsMainFragment
import com.zy.assemble.fragment.AlgorithmMainFragment
import com.zy.test.widget.fragment.TabLayoutFragment

class MainActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    private lateinit var fragments: Array<Fragment>
    private lateinit var viewPager: ViewPager
    private lateinit var radioGroup: RadioGroup


    private fun initView() {
        initFragment()
        viewPager = findViewById(com.zy.test.widget.R.id.fragment_container_viewpager)
        radioGroup = findViewById(com.zy.test.widget.R.id.nav_radio_group)
        viewPager.adapter = object :
            FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(position: Int): Fragment {
                return fragments[position]
            }

            override fun getPageTitle(position: Int): CharSequence {
                return "titles[$position]"
            }
        }
        initListener()

        viewPager.currentItem = 0
        radioGroup.check(com.zy.test.widget.R.id.nav_radio_button_1)
    }


    private fun initListener() {

        radioGroup.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) { //点击切换viewpager页面
                com.zy.test.widget.R.id.nav_radio_button_1 -> {
                    viewPager.currentItem = 0
                }
                com.zy.test.widget.R.id.nav_radio_button_2 -> {
                    viewPager.currentItem = 1
                }
                com.zy.test.widget.R.id.nav_radio_button_3 -> {
                    viewPager.currentItem = 2
                }
                com.zy.test.widget.R.id.nav_radio_button_4 -> {
                    viewPager.currentItem = 3
                }
            }
        }
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) { //当页面左右滑动发生改变时，radiogroup也改变子项的选择状态
                when (position) {
                    0 -> radioGroup.check(com.zy.test.widget.R.id.nav_radio_button_1)
                    1 -> radioGroup.check(com.zy.test.widget.R.id.nav_radio_button_2)
                    2 -> radioGroup.check(com.zy.test.widget.R.id.nav_radio_button_3)
                    3 -> radioGroup.check(com.zy.test.widget.R.id.nav_radio_button_4)
                }
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })
    }


    private fun initFragment() {
        fragments = arrayOf(
            AndroidFragment.newInstance(),
            AlgorithmMainFragment.newInstance(),
            TabLayoutFragment.newInstance("发现"),
            SettingsMainFragment.newInstance()
        )
    }

}